package com.baidu.baidumap;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;

public class HelloActivity extends Activity {

    private MapView mMapView;
    private static final String TAG = "HelloWorld";
    private MyBaduSdkReceiver baduSdkReceiver;
    private BaiduMap baiduMap;

    private double latitude = 32.050966;// 纬度
    private double longitude = 121.303128;// 经度
    private LatLng hmPos = new LatLng(latitude, longitude);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        initManager();
        setContentView(R.layout.common);
        init();

    }

    private void init() {
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        baiduMap = mMapView.getMap();
        //将描述地图状态将要发生的变化，使用工厂类MapStatusUpdateFactory创建
      /*  MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(19);
        //设置缩放级别
        baiduMap.setMapStatus(mapStatusUpdate);*/
        MapStatusUpdate mapStatusUpdatePoint = MapStatusUpdateFactory.newLatLng(hmPos);
        //设置zhong中心点默认是天安门
        baiduMap.setMapStatus(mapStatusUpdatePoint);
        // mapview.showZoomControls(false);// 默认是true 显示缩放按钮
        //
        // mapview.showScaleControl(false);// 默认是true 显示标尺
    }



        private void initManager() {
        //注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(getApplicationContext());
        baduSdkReceiver = new MyBaduSdkReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);// 注册网络错误
        filter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR); // 注册key校验结果
        registerReceiver(baduSdkReceiver, filter);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_1:
                // 放大
                // 放大地图缩放级别 每次放大一个级别
                MapStatusUpdate zoomInStatus = MapStatusUpdateFactory.zoomIn();
                baiduMap.setMapStatus(zoomInStatus);
                break;
            case KeyEvent.KEYCODE_2:
                // 缩小
                // 缩小地图缩放级别 每次缩小一个级别
                MapStatusUpdate zooOutStatus = MapStatusUpdateFactory.zoomOut();
                baiduMap.setMapStatus(zooOutStatus);
                break;
            case KeyEvent.KEYCODE_3:
                // 以一个点为中心 旋转
                MapStatus mapStatus = baiduMap.getMapStatus();// 获取地图的当前状态
                float rotate = mapStatus.rotate;
                Log.d(TAG, "rotate:" + rotate);
                // 旋转 范围0~360
                MapStatus rotateStatus = new  MapStatus.Builder().rotate(rotate+30).build();
                // 创建新的MapStatus
                MapStatusUpdate rotateStatusUpdate = MapStatusUpdateFactory.newMapStatus(rotateStatus);
                baiduMap.setMapStatus(rotateStatusUpdate);

                break;
            case KeyEvent.KEYCODE_4:
                // 已一条直线 为轴 旋转 Overlooking 俯角
                MapStatus mapStatusOver = baiduMap.getMapStatus();// 获取地图的当前状态
                float overlook = mapStatusOver.overlook;
                Log.d(TAG, "overlook:" + overlook);
                // 0~45
                MapStatus overStatus = new MapStatus.Builder().overlook(overlook-5).build();
                MapStatusUpdate overStatusUpdate = MapStatusUpdateFactory.newMapStatus(overStatus);
                baiduMap.setMapStatus(overStatusUpdate);
                break;
            case KeyEvent.KEYCODE_5:
                // 移动
                MapStatusUpdate moveStatusUpdate = MapStatusUpdateFactory.newLatLng(new LatLng(40.065796,116.349868));
                // 带动画的更新地图状态 耗时300毫秒
                baiduMap.animateMapStatus(moveStatusUpdate);
                break;

            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    class MyBaduSdkReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String result = intent.getAction();
            if (result.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)){
                Toast.makeText(getApplicationContext(),"wuwangluo",Toast.LENGTH_SHORT).show();
            }else if (result.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)){
                //key校验失败
                Toast.makeText(getApplicationContext(),"key校验失败",Toast.LENGTH_SHORT).show();
            }
        }
    }
}
